package ccmis.finance.voucher.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;

import ccmis.finance.voucher.bo.IvvoucherDisassembleBo;
import ccmis.finance.voucher.bo.VoucherBo;
import ccmis.finance.voucher.entity.IvvoucherDisassemble;
import ccmis.finance.voucher.entity.Voucher;
import jfox.platform.infrastructure.SuperAction;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;


@Service(id = "ivvoucherDisassemble")
public class IvvoucherDisassembleAction extends SuperAction{
	@EJB
	IvvoucherDisassembleBo ivvoucherDisassembleBo;
	@EJB
	VoucherBo voucherBo;
	 /**
	   * 增加投资类凭证
	   * @param actionContext
	   * @throws Exception
	   */
	  @ActionMethod(name="PreCvd", successView = "ccmis/finance/invest/add_invest.vhtml",invocationClass = PreDisassemble.class)
	   public void PreVoucherDisassemble(ActionContext actionContext) throws Exception{
		  PreDisassemble invocation = (PreDisassemble)actionContext.getInvocation();
			String voucherId = invocation.getPzId().trim();
			String dispartNo = invocation.getFlh().trim();
			BigDecimal budget_remain = new BigDecimal(0.00);
			BigDecimal lend_remain = new BigDecimal(0.00);
			Voucher vh=new Voucher();
			List<Voucher> vhs = voucherBo.getVoucherByPzIdFlh(voucherId, dispartNo);
			if (vhs != null && vhs.size() > 0){
				 vh=(Voucher) vhs.get(0);
				 budget_remain = vh.getDJE();
				 lend_remain = vh.getJje();
			}else{
				vh=null;
			}
			List<IvvoucherDisassemble> vd = ivvoucherDisassembleBo.getIVDisassembleByVidDno(voucherId, dispartNo);
			ArrayList vds= new ArrayList(vd);
			if (vds != null && vds.size() > 0)
				for (Iterator it = vds.iterator(); it.hasNext();) {
					IvvoucherDisassemble ivd = (IvvoucherDisassemble) it.next();
					if (ivd.getBudgetValue() != null)
						budget_remain = budget_remain.subtract(ivd.getBudgetValue());
					if (ivd.getLendValue()!= null)
						lend_remain = lend_remain.subtract(ivd.getLendValue());
				}
			
			PageContext pageContext = actionContext.getPageContext();
			pageContext.setAttribute("voucherId", voucherId);
	        pageContext.setAttribute("dispartNo", dispartNo);
	        pageContext.setAttribute("budget_remain", budget_remain);
	        pageContext.setAttribute("lend_remain", lend_remain);
	    }
	   public static class PreDisassemble extends Invocation{
	    	//凭证编号
	    	private String pzId;
	    	//分录号
	    	private String flh;

			public String getPzId() {
				return pzId;
			}

			public void setPzId(String pzId) {
				this.pzId = pzId;
			}
			
			public String getFlh() {
				return flh;
			}

			public void setFlh(String flh) {
				this.flh = flh;
			}
	    	
	    }
}
